package com.jabo.wechatmomentsdemo.http;

import android.util.Log;
import android.widget.Toast;

import com.jabo.wechatmomentsdemo.app.MomentsApplication;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @anthor JZheng3
 * @Time: 2020/9/4
 * 加一层封装，统一处理回调信息，比如统一的错误码处理
 **/
public abstract class AbstractCallback<T> implements Callback<T> {

    private static final String TAG = AbstractCallback.class.getName();

    @Override
    final public void onResponse(Call<T> call, Response<T> response) {
        //Maybe we can do something ...
        onHandleResponse(call, response);
        Log.e(TAG, "onResponse>>" + call.request().url().url().toString() + "\nonResponse: " + response.message());
    }

    @Override
    final public void onFailure(Call<T> call, Throwable t) {
        //we can do ExceptionHandle
        String errorMessage = ExceptionHandle.handleException(t);
        onHandleFailure(call, t, errorMessage);
        Toast.makeText(MomentsApplication.getInstance(), errorMessage, Toast.LENGTH_SHORT).show();
        Log.e(TAG, "onFailure>>" + call.request().url().url().toString() + "\nerrorMessage: " + errorMessage);
    }

    protected void onHandleFailure(Call<T> call, Throwable t, String errorMessage){};

    protected abstract void onHandleResponse(Call<T> call, Response<T> response);
}
